2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_slaveRXMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_B I2C - Slave receive multiple bytes
34  *
35  * Description: In this example, the device is configured as an I2C slave. It
36  * receives bytes over I2C and stores them in an array for further inspect. The
37  * device stays in LPM0 for the entire interaction. This example can be
38  * modified to change the length of received bytes. Start this example
39  * before running the corresponding master code.
40  *
41  *
42  * /|\ /|\
43  * MSP430i2041 10k 10k MSP430i2041
44  * slave | | master
45  * ----------------- | | -----------------
46  * | P1.6/UCB0SCL|<-|----+->|P1.6/UCB0SCL |
47  * | | | | |
48  * | | | | |
49  * | P1.7/UCB0SDA|<-+------>|P1.7/UCB0SDA |
50  * | | | |
51  *
52  * Author: Zack Lalanne
53  ******************************************************************************/
54 
55 #include "driverlib.h"
56 
57 #define SLAVE_ADDRESS 0x48
58 #define NUM_OF_RX_BYTES 4
59 
60 static volatile uint8_t RXData[NUM_OF_RX_BYTES];
61 static volatile uint8_t RXDataIndex;
62 
63 int main(void) {
64 
65  EUSCI_B_I2C_initSlaveParam i2cConfig = {
66  SLAVE_ADDRESS, // Slave Address
67  EUSCI_B_I2C_OWN_ADDRESS_OFFSET0,
68  EUSCI_B_I2C_OWN_ADDRESS_ENABLE
69  };
70 
71  WDT_hold(WDT_BASE);
72 
73  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
74  CS_setupDCO(CS_INTERNAL_RESISTOR);
75 
76 
77  // Setting P1.6 and P1.7 as I2C pins
78  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
79  GPIO_PIN6 | GPIO_PIN7,
80  GPIO_PRIMARY_MODULE_FUNCTION);
81 
82  // Setting up I2C communication as slave
83  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &i2cConfig);
84 
85  // Set slave in receive mode
86  EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE);
87 
88  // Enable the module for operation
89  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
90 
91  // Enable needed I2C interrupts
92  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
93  EUSCI_B_I2C_RECEIVE_INTERRUPT0);
94  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
95  EUSCI_B_I2C_RECEIVE_INTERRUPT0);
96 
97  // Go to sleep and wait for LPM exit
98  __bis_SR_register(LPM0_bits | GIE);
99 }
100 
101 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
102 #pragma vector=USCI_B0_VECTOR
103 __interrupt
104 #elif defined(__GNUC__)
105 __attribute__((interrupt(USCI_B0_VECTOR)))
106 #endif
107 void USCIB0_ISR(void) {
108  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG)) {
109  case USCI_NONE: break;
110  case USCI_I2C_UCALIFG: break;
111  case USCI_I2C_UCNACKIFG:
112  case USCI_I2C_UCSTTIFG: break;
113  case USCI_I2C_UCSTPIFG: break;
114  case USCI_I2C_UCRXIFG3: break;
115  case USCI_I2C_UCTXIFG3: break;
116  case USCI_I2C_UCRXIFG2: break;
117  case USCI_I2C_UCTXIFG2: break;
118  case USCI_I2C_UCRXIFG1: break;
119  case USCI_I2C_UCTXIFG1: break;
120  case USCI_I2C_UCRXIFG0:
121  RXData[RXDataIndex++] = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
122 
123  // Reset index if at end of array
124  if(RXDataIndex == NUM_OF_RX_BYTES) {
125  RXDataIndex = 0;
126  }
127 
128  break;
129  case USCI_I2C_UCTXIFG0: break;
130  case USCI_I2C_UCBCNTIFG: break;
131  case USCI_I2C_UCCLTOIFG: break;
132  case USCI_I2C_UCBIT9IFG: break;
133  default: break;
134  }
135 }
uint8_t RXData
void USCIB0_ISR(void)
#define NUM_OF_RX_BYTES
#define SLAVE_ADDRESS